
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import dados.Conta;
import dados.Lancamento;
import dados.OrdenacaoPorConta;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.ObjectSet;
import net.sourceforge.floggy.persistence.PersistableManager;

public class FinanceiroMIDlet extends MIDlet implements CommandListener {

  private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
private Command cmdSair;
private Command cmdNovaConta;
private Command cmdLancamento;
private Command cmdVoltar1;
private Command cmdCancelar;
private Command okCommand;
private Command cmdVoltar;
private Command cmdRelatorio;
private Command cmdGravarConta;
private Command cmdGravarLcto;
private Command cmdGerarRelatorio;
private Command cmdVoltar2;
private Command cmdVoltar3;
private Command backCommand;
private Command okCommand1;
private Form FrmFinanceiro;
private ImageItem imageItem;
private Form FrmLctoConta;
private TextField tfComplemento;
private TextField tfValor;
private StringItem siConta;
private DateField dfData;
private Form FrmCadConta;
private ChoiceGroup cgTipoConta;
private TextField tfNomeConta;
private List listConta;
private Form frmRelContas;
private DateField dfInicial;
private ChoiceGroup choiceGroup;
private DateField dfFinal;
private List listRelContas;
private Image image1;
//</editor-fold>//GEN-END:|fields|0|

  public FinanceiroMIDlet() {
  }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
/**
 * Initializes the application.
 * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
 */
private void initialize () {//GEN-END:|0-initialize|0|0-preInitialize
  // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
  // write post-initialize user code here
}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
/**
 * Performs an action assigned to the Mobile Device - MIDlet Started point.
 */
public void startMIDlet () {//GEN-END:|3-startMIDlet|0|3-preAction
  // write pre-action user code here
switchDisplayable (null, getFrmFinanceiro ());//GEN-LINE:|3-startMIDlet|1|3-postAction
  // write post-action user code here
}//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
/**
 * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
 */
public void resumeMIDlet () {//GEN-END:|4-resumeMIDlet|0|4-preAction
  // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
  // write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
/**
 * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
 * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
 * @param nextDisplayable the Displayable to be set
 */
public void switchDisplayable (Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
  // write pre-switch user code here
Display display = getDisplay ();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
if (alert == null) {
display.setCurrent (nextDisplayable);
} else {
display.setCurrent (alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
  // write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
 * Called by a system to indicated that a command has been invoked on a particular displayable.
 * @param command the Command that was invoked
 * @param displayable the Displayable where the command was invoked
 */
public void commandAction (Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
  // write pre-action user code here
if (displayable == FrmCadConta) {//GEN-BEGIN:|7-commandAction|1|65-preAction
if (command == cmdGravarConta) {//GEN-END:|7-commandAction|1|65-preAction
  gravarConta();
//GEN-LINE:|7-commandAction|2|65-postAction

} else if (command == cmdVoltar1) {//GEN-LINE:|7-commandAction|3|36-preAction

switchDisplayable (null, getFrmFinanceiro ());//GEN-LINE:|7-commandAction|4|36-postAction

}//GEN-BEGIN:|7-commandAction|5|32-preAction
} else if (displayable == FrmFinanceiro) {
if (command == cmdLancamento) {//GEN-END:|7-commandAction|5|32-preAction
  listarContas();
//GEN-LINE:|7-commandAction|6|32-postAction

} else if (command == cmdNovaConta) {//GEN-LINE:|7-commandAction|7|30-preAction

switchDisplayable (null, getFrmCadConta ());//GEN-LINE:|7-commandAction|8|30-postAction

} else if (command == cmdRelatorio) {//GEN-LINE:|7-commandAction|9|58-preAction
switchDisplayable (null, getFrmRelContas ());//GEN-LINE:|7-commandAction|10|58-postAction
} else if (command == cmdSair) {//GEN-LINE:|7-commandAction|11|19-preAction

exitMIDlet ();//GEN-LINE:|7-commandAction|12|19-postAction

}//GEN-BEGIN:|7-commandAction|13|39-preAction
} else if (displayable == FrmLctoConta) {
if (command == cmdCancelar) {//GEN-END:|7-commandAction|13|39-preAction

switchDisplayable (null, getFrmFinanceiro ());//GEN-LINE:|7-commandAction|14|39-postAction

} else if (command == cmdGravarLcto) {//GEN-LINE:|7-commandAction|15|67-preAction
  gravarLancamento();
//GEN-LINE:|7-commandAction|16|67-postAction

}//GEN-BEGIN:|7-commandAction|17|76-preAction
} else if (displayable == frmRelContas) {
if (command == cmdGerarRelatorio) {//GEN-END:|7-commandAction|17|76-preAction
  // write pre-action user code here
  gerarRelatorio();
//GEN-LINE:|7-commandAction|18|76-postAction
  // write post-action user code here
} else if (command == cmdVoltar2) {//GEN-LINE:|7-commandAction|19|78-preAction
  // write pre-action user code here
switchDisplayable (null, getFrmFinanceiro ());//GEN-LINE:|7-commandAction|20|78-postAction
  // write post-action user code here
}//GEN-BEGIN:|7-commandAction|21|46-preAction
} else if (displayable == listConta) {
if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|21|46-preAction
  // write pre-action user code here
listContaAction ();//GEN-LINE:|7-commandAction|22|46-postAction
  // write post-action user code here
} else if (command == cmdVoltar3) {//GEN-LINE:|7-commandAction|23|80-preAction
  // write pre-action user code here
switchDisplayable (null, getFrmFinanceiro ());//GEN-LINE:|7-commandAction|24|80-postAction
  // write post-action user code here
} else if (command == okCommand) {//GEN-LINE:|7-commandAction|25|52-preAction
  // write pre-action user code here
switchDisplayable (null, getFrmLctoConta ());//GEN-LINE:|7-commandAction|26|52-postAction
  // write post-action user code here
}//GEN-BEGIN:|7-commandAction|27|90-preAction
} else if (displayable == listRelContas) {
if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|27|90-preAction
  // write pre-action user code here
listRelContasAction ();//GEN-LINE:|7-commandAction|28|90-postAction
  // write post-action user code here
} else if (command == backCommand) {//GEN-LINE:|7-commandAction|29|93-preAction
  // write pre-action user code here
switchDisplayable (null, getFrmRelContas ());//GEN-LINE:|7-commandAction|30|93-postAction
  // write post-action user code here
} else if (command == okCommand1) {//GEN-LINE:|7-commandAction|31|96-preAction
  // write pre-action user code here
switchDisplayable (null, getFrmFinanceiro ());//GEN-LINE:|7-commandAction|32|96-postAction
  // write post-action user code here
}//GEN-BEGIN:|7-commandAction|33|7-postCommandAction
}//GEN-END:|7-commandAction|33|7-postCommandAction
  // write post-action user code here
}//GEN-BEGIN:|7-commandAction|34|
//</editor-fold>//GEN-END:|7-commandAction|34|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSair ">//GEN-BEGIN:|18-getter|0|18-preInit
/**
 * Returns an initiliazed instance of cmdSair component.
 * @return the initialized component instance
 */
public Command getCmdSair () {
if (cmdSair == null) {//GEN-END:|18-getter|0|18-preInit
  // write pre-init user code here
cmdSair = new Command ("Sair", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
  // write post-init user code here
}//GEN-BEGIN:|18-getter|2|
return cmdSair;
}
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: FrmFinanceiro ">//GEN-BEGIN:|14-getter|0|14-preInit
/**
 * Returns an initiliazed instance of FrmFinanceiro component.
 * @return the initialized component instance
 */
public Form getFrmFinanceiro () {
if (FrmFinanceiro == null) {//GEN-END:|14-getter|0|14-preInit
  // write pre-init user code here
FrmFinanceiro = new Form ("Financeiro", new Item[] { getImageItem () });//GEN-BEGIN:|14-getter|1|14-postInit
FrmFinanceiro.addCommand (getCmdSair ());
FrmFinanceiro.addCommand (getCmdLancamento ());
FrmFinanceiro.addCommand (getCmdNovaConta ());
FrmFinanceiro.addCommand (getCmdRelatorio ());
FrmFinanceiro.setCommandListener (this);//GEN-END:|14-getter|1|14-postInit
  // write post-init user code here
}//GEN-BEGIN:|14-getter|2|
return FrmFinanceiro;
}
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdNovaConta ">//GEN-BEGIN:|29-getter|0|29-preInit
/**
 * Returns an initiliazed instance of cmdNovaConta component.
 * @return the initialized component instance
 */
public Command getCmdNovaConta () {
if (cmdNovaConta == null) {//GEN-END:|29-getter|0|29-preInit
  // write pre-init user code here
cmdNovaConta = new Command ("Nova Conta", Command.ITEM, 0);//GEN-LINE:|29-getter|1|29-postInit
  // write post-init user code here
}//GEN-BEGIN:|29-getter|2|
return cmdNovaConta;
}
//</editor-fold>//GEN-END:|29-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdLancamento ">//GEN-BEGIN:|31-getter|0|31-preInit
/**
 * Returns an initiliazed instance of cmdLancamento component.
 * @return the initialized component instance
 */
public Command getCmdLancamento () {
if (cmdLancamento == null) {//GEN-END:|31-getter|0|31-preInit
  // write pre-init user code here
cmdLancamento = new Command ("Lan\u00E7amento", Command.ITEM, 0);//GEN-LINE:|31-getter|1|31-postInit
  // write post-init user code here
}//GEN-BEGIN:|31-getter|2|
return cmdLancamento;
}
//</editor-fold>//GEN-END:|31-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdVoltar1 ">//GEN-BEGIN:|35-getter|0|35-preInit
/**
 * Returns an initiliazed instance of cmdVoltar1 component.
 * @return the initialized component instance
 */
public Command getCmdVoltar1 () {
if (cmdVoltar1 == null) {//GEN-END:|35-getter|0|35-preInit
  // write pre-init user code here
cmdVoltar1 = new Command ("Voltar", Command.BACK, 0);//GEN-LINE:|35-getter|1|35-postInit
  // write post-init user code here
}//GEN-BEGIN:|35-getter|2|
return cmdVoltar1;
}
//</editor-fold>//GEN-END:|35-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdCancelar ">//GEN-BEGIN:|38-getter|0|38-preInit
/**
 * Returns an initiliazed instance of cmdCancelar component.
 * @return the initialized component instance
 */
public Command getCmdCancelar () {
if (cmdCancelar == null) {//GEN-END:|38-getter|0|38-preInit
  // write pre-init user code here
cmdCancelar = new Command ("Cancelar", Command.CANCEL, 0);//GEN-LINE:|38-getter|1|38-postInit
  // write post-init user code here
}//GEN-BEGIN:|38-getter|2|
return cmdCancelar;
}
//</editor-fold>//GEN-END:|38-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: FrmCadConta ">//GEN-BEGIN:|22-getter|0|22-preInit
/**
 * Returns an initiliazed instance of FrmCadConta component.
 * @return the initialized component instance
 */
public Form getFrmCadConta () {
if (FrmCadConta == null) {//GEN-END:|22-getter|0|22-preInit
  // write pre-init user code here
FrmCadConta = new Form ("Cadastro de Conta", new Item[] { getTfNomeConta (), getCgTipoConta () });//GEN-BEGIN:|22-getter|1|22-postInit
FrmCadConta.addCommand (getCmdVoltar1 ());
FrmCadConta.addCommand (getCmdGravarConta ());
FrmCadConta.setCommandListener (this);//GEN-END:|22-getter|1|22-postInit
  // write post-init user code here
}//GEN-BEGIN:|22-getter|2|
return FrmCadConta;
}
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cgTipoConta ">//GEN-BEGIN:|24-getter|0|24-preInit
/**
 * Returns an initiliazed instance of cgTipoConta component.
 * @return the initialized component instance
 */
public ChoiceGroup getCgTipoConta () {
if (cgTipoConta == null) {//GEN-END:|24-getter|0|24-preInit
  // write pre-init user code here
cgTipoConta = new ChoiceGroup ("Tipo de Conta", Choice.EXCLUSIVE);//GEN-BEGIN:|24-getter|1|24-postInit
cgTipoConta.append ("Cr\u00E9dito", null);
cgTipoConta.append ("D\u00E9bito", null);
cgTipoConta.setSelectedFlags (new boolean[] { false, false });//GEN-END:|24-getter|1|24-postInit
  // write post-init user code here
}//GEN-BEGIN:|24-getter|2|
return cgTipoConta;
}
//</editor-fold>//GEN-END:|24-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfNomeConta ">//GEN-BEGIN:|27-getter|0|27-preInit
/**
 * Returns an initiliazed instance of tfNomeConta component.
 * @return the initialized component instance
 */
public TextField getTfNomeConta () {
if (tfNomeConta == null) {//GEN-END:|27-getter|0|27-preInit
  // write pre-init user code here
tfNomeConta = new TextField ("Nome da Conta:", null, 32, TextField.ANY);//GEN-LINE:|27-getter|1|27-postInit
  // write post-init user code here
}//GEN-BEGIN:|27-getter|2|
return tfNomeConta;
}
//</editor-fold>//GEN-END:|27-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: FrmLctoConta ">//GEN-BEGIN:|23-getter|0|23-preInit
/**
 * Returns an initiliazed instance of FrmLctoConta component.
 * @return the initialized component instance
 */
public Form getFrmLctoConta () {
if (FrmLctoConta == null) {//GEN-END:|23-getter|0|23-preInit
  // write pre-init user code here
FrmLctoConta = new Form ("Lan\u00E7amento de Conta", new Item[] { getSiConta (), getDfData (), getTfValor (), getTfComplemento () });//GEN-BEGIN:|23-getter|1|23-postInit
FrmLctoConta.addCommand (getCmdCancelar ());
FrmLctoConta.addCommand (getCmdGravarLcto ());
FrmLctoConta.setCommandListener (this);//GEN-END:|23-getter|1|23-postInit
  // write post-init user code here
  siConta.setText(listConta.getString(listConta.getSelectedIndex()));
}//GEN-BEGIN:|23-getter|2|
return FrmLctoConta;
}
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfComplemento ">//GEN-BEGIN:|28-getter|0|28-preInit
/**
 * Returns an initiliazed instance of tfComplemento component.
 * @return the initialized component instance
 */
public TextField getTfComplemento () {
if (tfComplemento == null) {//GEN-END:|28-getter|0|28-preInit
  // write pre-init user code here
tfComplemento = new TextField ("Complemento:", null, 32, TextField.ANY);//GEN-LINE:|28-getter|1|28-postInit
  // write post-init user code here
}//GEN-BEGIN:|28-getter|2|
return tfComplemento;
}
//</editor-fold>//GEN-END:|28-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfValor ">//GEN-BEGIN:|41-getter|0|41-preInit
/**
 * Returns an initiliazed instance of tfValor component.
 * @return the initialized component instance
 */
public TextField getTfValor () {
if (tfValor == null) {//GEN-END:|41-getter|0|41-preInit
  // write pre-init user code here
tfValor = new TextField ("Valor", null, 32, TextField.DECIMAL);//GEN-LINE:|41-getter|1|41-postInit
  // write post-init user code here
}//GEN-BEGIN:|41-getter|2|
return tfValor;
}
//</editor-fold>//GEN-END:|41-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdVoltar ">//GEN-BEGIN:|49-getter|0|49-preInit
/**
 * Returns an initiliazed instance of cmdVoltar component.
 * @return the initialized component instance
 */
public Command getCmdVoltar () {
if (cmdVoltar == null) {//GEN-END:|49-getter|0|49-preInit
  // write pre-init user code here
cmdVoltar = new Command ("Voltar1", Command.BACK, 0);//GEN-LINE:|49-getter|1|49-postInit
  // write post-init user code here
}//GEN-BEGIN:|49-getter|2|
return cmdVoltar;
}
//</editor-fold>//GEN-END:|49-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|51-getter|0|51-preInit
/**
 * Returns an initiliazed instance of okCommand component.
 * @return the initialized component instance
 */
public Command getOkCommand () {
if (okCommand == null) {//GEN-END:|51-getter|0|51-preInit
  // write pre-init user code here
okCommand = new Command ("OK", "<nulo>", Command.OK, 0);//GEN-LINE:|51-getter|1|51-postInit
  // write post-init user code here
}//GEN-BEGIN:|51-getter|2|
return okCommand;
}
//</editor-fold>//GEN-END:|51-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: listConta ">//GEN-BEGIN:|44-getter|0|44-preInit
/**
 * Returns an initiliazed instance of listConta component.
 * @return the initialized component instance
 */
public List getListConta () {
if (listConta == null) {//GEN-END:|44-getter|0|44-preInit
  // write pre-init user code here
listConta = new List ("Escolha a Conta", Choice.EXCLUSIVE);//GEN-BEGIN:|44-getter|1|44-postInit
listConta.addCommand (getOkCommand ());
listConta.addCommand (getCmdVoltar3 ());
listConta.setCommandListener (this);
listConta.setFitPolicy (Choice.TEXT_WRAP_DEFAULT);//GEN-END:|44-getter|1|44-postInit
  // write post-init user code here
}//GEN-BEGIN:|44-getter|2|
return listConta;
}
//</editor-fold>//GEN-END:|44-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: listContaAction ">//GEN-BEGIN:|44-action|0|44-preAction
/**
 * Performs an action assigned to the selected list element in the listConta component.
 */
public void listContaAction () {//GEN-END:|44-action|0|44-preAction
  // enter pre-action user code here
String __selectedString = getListConta ().getString (getListConta ().getSelectedIndex ());//GEN-LINE:|44-action|1|44-postAction
  // enter post-action user code here
}//GEN-BEGIN:|44-action|2|
//</editor-fold>//GEN-END:|44-action|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdRelatorio ">//GEN-BEGIN:|57-getter|0|57-preInit
/**
 * Returns an initiliazed instance of cmdRelatorio component.
 * @return the initialized component instance
 */
public Command getCmdRelatorio () {
if (cmdRelatorio == null) {//GEN-END:|57-getter|0|57-preInit
  // write pre-init user code here
cmdRelatorio = new Command ("Relat\u00F3rio", Command.ITEM, 0);//GEN-LINE:|57-getter|1|57-postInit
  // write post-init user code here
}//GEN-BEGIN:|57-getter|2|
return cmdRelatorio;
}
//</editor-fold>//GEN-END:|57-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageItem ">//GEN-BEGIN:|61-getter|0|61-preInit
/**
 * Returns an initiliazed instance of imageItem component.
 * @return the initialized component instance
 */
public ImageItem getImageItem () {
if (imageItem == null) {//GEN-END:|61-getter|0|61-preInit
  // write pre-init user code here
imageItem = new ImageItem ("", getImage1 (), ImageItem.LAYOUT_DEFAULT, "<Imagem perdida>");//GEN-LINE:|61-getter|1|61-postInit
  // write post-init user code here
}//GEN-BEGIN:|61-getter|2|
return imageItem;
}
//</editor-fold>//GEN-END:|61-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|62-getter|0|62-preInit
/**
 * Returns an initiliazed instance of image1 component.
 * @return the initialized component instance
 */
public Image getImage1 () {
if (image1 == null) {//GEN-END:|62-getter|0|62-preInit
  // write pre-init user code here
try {//GEN-BEGIN:|62-getter|1|62-@java.io.IOException
image1 = Image.createImage ("/logo.jpg");
} catch (java.io.IOException e) {//GEN-END:|62-getter|1|62-@java.io.IOException
  e.printStackTrace();
}//GEN-LINE:|62-getter|2|62-postInit
  // write post-init user code here
}//GEN-BEGIN:|62-getter|3|
return image1;
}
//</editor-fold>//GEN-END:|62-getter|3|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdGravarConta ">//GEN-BEGIN:|64-getter|0|64-preInit
/**
 * Returns an initiliazed instance of cmdGravarConta component.
 * @return the initialized component instance
 */
public Command getCmdGravarConta () {
if (cmdGravarConta == null) {//GEN-END:|64-getter|0|64-preInit
  // write pre-init user code here
cmdGravarConta = new Command ("Gravar", Command.ITEM, 0);//GEN-LINE:|64-getter|1|64-postInit
  // write post-init user code here
}//GEN-BEGIN:|64-getter|2|
return cmdGravarConta;
}
//</editor-fold>//GEN-END:|64-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdGravarLcto ">//GEN-BEGIN:|66-getter|0|66-preInit
/**
 * Returns an initiliazed instance of cmdGravarLcto component.
 * @return the initialized component instance
 */
public Command getCmdGravarLcto () {
if (cmdGravarLcto == null) {//GEN-END:|66-getter|0|66-preInit
  // write pre-init user code here
cmdGravarLcto = new Command ("Gravar", Command.ITEM, 0);//GEN-LINE:|66-getter|1|66-postInit
  // write post-init user code here
}//GEN-BEGIN:|66-getter|2|
return cmdGravarLcto;
}
//</editor-fold>//GEN-END:|66-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: siConta ">//GEN-BEGIN:|68-getter|0|68-preInit
/**
 * Returns an initiliazed instance of siConta component.
 * @return the initialized component instance
 */
public StringItem getSiConta () {
if (siConta == null) {//GEN-END:|68-getter|0|68-preInit
  // write pre-init user code here
siConta = new StringItem ("Conta:", null);//GEN-LINE:|68-getter|1|68-postInit
  // write post-init user code here
}//GEN-BEGIN:|68-getter|2|
return siConta;
}
//</editor-fold>//GEN-END:|68-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: dfData ">//GEN-BEGIN:|69-getter|0|69-preInit
/**
 * Returns an initiliazed instance of dfData component.
 * @return the initialized component instance
 */
public DateField getDfData () {
if (dfData == null) {//GEN-END:|69-getter|0|69-preInit
  // write pre-init user code here
dfData = new DateField ("Data", DateField.DATE_TIME);//GEN-BEGIN:|69-getter|1|69-postInit
dfData.setDate (new java.util.Date (System.currentTimeMillis ()));//GEN-END:|69-getter|1|69-postInit
  // write post-init user code here
}//GEN-BEGIN:|69-getter|2|
return dfData;
}
//</editor-fold>//GEN-END:|69-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmRelContas ">//GEN-BEGIN:|70-getter|0|70-preInit
/**
 * Returns an initiliazed instance of frmRelContas component.
 * @return the initialized component instance
 */
public Form getFrmRelContas () {
if (frmRelContas == null) {//GEN-END:|70-getter|0|70-preInit
  // write pre-init user code here
frmRelContas = new Form ("Filtros para Relat\u00F3rio", new Item[] { getDfInicial (), getDfFinal (), getChoiceGroup () });//GEN-BEGIN:|70-getter|1|70-postInit
frmRelContas.addCommand (getCmdGerarRelatorio ());
frmRelContas.addCommand (getCmdVoltar2 ());
frmRelContas.setCommandListener (this);//GEN-END:|70-getter|1|70-postInit
  // write post-init user code here
}//GEN-BEGIN:|70-getter|2|
return frmRelContas;
}
//</editor-fold>//GEN-END:|70-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdGerarRelatorio ">//GEN-BEGIN:|75-getter|0|75-preInit
/**
 * Returns an initiliazed instance of cmdGerarRelatorio component.
 * @return the initialized component instance
 */
public Command getCmdGerarRelatorio () {
if (cmdGerarRelatorio == null) {//GEN-END:|75-getter|0|75-preInit
  // write pre-init user code here
cmdGerarRelatorio = new Command ("Gerar Relat\u00F3rio", Command.ITEM, 0);//GEN-LINE:|75-getter|1|75-postInit
  // write post-init user code here
}//GEN-BEGIN:|75-getter|2|
return cmdGerarRelatorio;
}
//</editor-fold>//GEN-END:|75-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdVoltar2 ">//GEN-BEGIN:|77-getter|0|77-preInit
/**
 * Returns an initiliazed instance of cmdVoltar2 component.
 * @return the initialized component instance
 */
public Command getCmdVoltar2 () {
if (cmdVoltar2 == null) {//GEN-END:|77-getter|0|77-preInit
  // write pre-init user code here
cmdVoltar2 = new Command ("Voltar", Command.BACK, 0);//GEN-LINE:|77-getter|1|77-postInit
  // write post-init user code here
}//GEN-BEGIN:|77-getter|2|
return cmdVoltar2;
}
//</editor-fold>//GEN-END:|77-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdVoltar3 ">//GEN-BEGIN:|79-getter|0|79-preInit
/**
 * Returns an initiliazed instance of cmdVoltar3 component.
 * @return the initialized component instance
 */
public Command getCmdVoltar3 () {
if (cmdVoltar3 == null) {//GEN-END:|79-getter|0|79-preInit
  // write pre-init user code here
cmdVoltar3 = new Command ("Voltar", Command.BACK, 0);//GEN-LINE:|79-getter|1|79-postInit
  // write post-init user code here
}//GEN-BEGIN:|79-getter|2|
return cmdVoltar3;
}
//</editor-fold>//GEN-END:|79-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: dfInicial ">//GEN-BEGIN:|83-getter|0|83-preInit
/**
 * Returns an initiliazed instance of dfInicial component.
 * @return the initialized component instance
 */
public DateField getDfInicial () {
if (dfInicial == null) {//GEN-END:|83-getter|0|83-preInit
  // write pre-init user code here
dfInicial = new DateField ("Data Inicial:", DateField.DATE);//GEN-BEGIN:|83-getter|1|83-postInit
dfInicial.setDate (new java.util.Date (System.currentTimeMillis ()));//GEN-END:|83-getter|1|83-postInit
  // write post-init user code here
}//GEN-BEGIN:|83-getter|2|
return dfInicial;
}
//</editor-fold>//GEN-END:|83-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">//GEN-BEGIN:|84-getter|0|84-preInit
/**
 * Returns an initiliazed instance of choiceGroup component.
 * @return the initialized component instance
 */
public ChoiceGroup getChoiceGroup () {
if (choiceGroup == null) {//GEN-END:|84-getter|0|84-preInit
  // write pre-init user code here
choiceGroup = new ChoiceGroup ("", Choice.MULTIPLE);//GEN-BEGIN:|84-getter|1|84-postInit
choiceGroup.append ("Agrupar por Conta", null);
choiceGroup.setSelectedFlags (new boolean[] { false });//GEN-END:|84-getter|1|84-postInit
  // write post-init user code here
}//GEN-BEGIN:|84-getter|2|
return choiceGroup;
}
//</editor-fold>//GEN-END:|84-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: dfFinal ">//GEN-BEGIN:|88-getter|0|88-preInit
/**
 * Returns an initiliazed instance of dfFinal component.
 * @return the initialized component instance
 */
public DateField getDfFinal () {
if (dfFinal == null) {//GEN-END:|88-getter|0|88-preInit
  // write pre-init user code here
dfFinal = new DateField ("Data Final:", DateField.DATE);//GEN-BEGIN:|88-getter|1|88-postInit
dfFinal.setDate (new java.util.Date (System.currentTimeMillis ()));//GEN-END:|88-getter|1|88-postInit
  // write post-init user code here
}//GEN-BEGIN:|88-getter|2|
return dfFinal;
}
//</editor-fold>//GEN-END:|88-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|92-getter|0|92-preInit
/**
 * Returns an initiliazed instance of backCommand component.
 * @return the initialized component instance
 */
public Command getBackCommand () {
if (backCommand == null) {//GEN-END:|92-getter|0|92-preInit
  // write pre-init user code here
backCommand = new Command ("Voltar", Command.BACK, 0);//GEN-LINE:|92-getter|1|92-postInit
  // write post-init user code here
}//GEN-BEGIN:|92-getter|2|
return backCommand;
}
//</editor-fold>//GEN-END:|92-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|95-getter|0|95-preInit
/**
 * Returns an initiliazed instance of okCommand1 component.
 * @return the initialized component instance
 */
public Command getOkCommand1 () {
if (okCommand1 == null) {//GEN-END:|95-getter|0|95-preInit
  // write pre-init user code here
okCommand1 = new Command ("Ok", Command.OK, 0);//GEN-LINE:|95-getter|1|95-postInit
  // write post-init user code here
}//GEN-BEGIN:|95-getter|2|
return okCommand1;
}
//</editor-fold>//GEN-END:|95-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: listRelContas ">//GEN-BEGIN:|89-getter|0|89-preInit
/**
 * Returns an initiliazed instance of listRelContas component.
 * @return the initialized component instance
 */
public List getListRelContas () {
if (listRelContas == null) {//GEN-END:|89-getter|0|89-preInit
  // write pre-init user code here
listRelContas = new List ("Relat\u00F3rio", Choice.IMPLICIT);//GEN-BEGIN:|89-getter|1|89-postInit
listRelContas.addCommand (getBackCommand ());
listRelContas.addCommand (getOkCommand1 ());
listRelContas.setCommandListener (this);//GEN-END:|89-getter|1|89-postInit
  // write post-init user code here
}//GEN-BEGIN:|89-getter|2|
return listRelContas;
}
//</editor-fold>//GEN-END:|89-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: listRelContasAction ">//GEN-BEGIN:|89-action|0|89-preAction
/**
 * Performs an action assigned to the selected list element in the listRelContas component.
 */
public void listRelContasAction () {//GEN-END:|89-action|0|89-preAction
  // enter pre-action user code here
String __selectedString = getListRelContas ().getString (getListRelContas ().getSelectedIndex ());//GEN-LINE:|89-action|1|89-postAction
  // enter post-action user code here
}//GEN-BEGIN:|89-action|2|
//</editor-fold>//GEN-END:|89-action|2|

  public Display getDisplay() {
    return Display.getDisplay(this);
  }

  public void exitMIDlet() {
    switchDisplayable(null, null);
    destroyApp(true);
    notifyDestroyed();
  }

  public void startApp() {
    if (midletPaused) {
      resumeMIDlet();
    } else {
      initialize();
      startMIDlet();
    }
    midletPaused = false;
  }

  public void pauseApp() {
    midletPaused = true;
  }

  public void destroyApp(boolean unconditional) {
    try {
      PersistableManager pm = PersistableManager.getInstance();
      pm.shutdown();
    } catch (FloggyException ex) {
      msgErro(ex.getMessage());
    }
  }

  private void msgErro(String msg) {
    Displayable d = getDisplay().getCurrent();
    Alert a = new Alert("Erro", msg, null, AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    getDisplay().setCurrent(a, d);
  }

  private void msgAviso(String msg) {
    Displayable d = getDisplay().getCurrent();
    Alert a = new Alert("Atenção", msg, null, AlertType.INFO);
    a.setTimeout(Alert.FOREVER);
    getDisplay().setCurrent(a, d);
  }

  private void gravarConta() {
    if (tfNomeConta.getString().equals("")) {
      msgAviso("Informe o Nome da Conta!");
      return;
    }

    Conta c = new Conta();
    if (c.find(tfNomeConta.getString())) {
      msgAviso("Esta Conta já Existe!");
      return;
    }

    c.setNomeConta(tfNomeConta.getString());
    if (cgTipoConta.getSelectedIndex() > 0) {
      c.setTipoCoonta(cgTipoConta.getSelectedIndex());
    }
    c.save();

    switchDisplayable(null, getFrmFinanceiro());
  }
  private ObjectSet contas = null;

  private void listarContas() {
    List l = getListConta();
    try {
      PersistableManager pm = PersistableManager.getInstance();
      contas = pm.find(Conta.class, null, new OrdenacaoPorConta());

      if (contas.size() == 0) {
        msgAviso("Nenhuma Conta Cadastrada!");
        return;
      }

      l.deleteAll();
      for (int i = 0; i < contas.size(); i++) {
        Conta c = (Conta) contas.get(i);
        l.append(c.getNomeConta(), null);
      }
    } catch (FloggyException ex) {
      msgErro(ex.getMessage());
    }

    switchDisplayable(null, l);
  }

  private void gravarLancamento() {
    double d = 0;
    try {
      d = Double.parseDouble(tfValor.getString());
    } catch (NumberFormatException e) {
      msgErro(e.getMessage());
    }
    
    if (d <= 0){
      msgAviso("Informe o Valor!");
      return;
    }
    
    Lancamento l = new Lancamento();

    try {
      Conta c = (Conta) contas.get(getListConta().getSelectedIndex());
      l.setConta(c);
      l.setValor(d);
      l.setData(dfData.getDate());
      l.setComplemento(tfComplemento.getString());
      l.save();
    } catch (FloggyException ex) {
      msgErro(ex.getMessage());
    }

    switchDisplayable(null, getFrmFinanceiro());
  }

  private void gerarRelatorio() {
    double cred = 0;
    double deb = 0;

    List l = getListRelContas();
    ObjectSet lancamentos = null;
    try {
      PersistableManager pm = PersistableManager.getInstance();
      //lancamentos = pm.find(Lancamento.class, new FilterLancamento(dfInicial.getDate(), dfFinal.getDate()), new OrdenacaoPorLancamento());
      lancamentos = pm.find(Lancamento.class, null, null);

      l.deleteAll();
      for (int i = 0; i < lancamentos.size(); i++) {
        Lancamento lc = (Lancamento) lancamentos.get(i);
        switch (lc.getConta().getTipoCoonta()) {
          case 0: {
            cred = cred + lc.getValor();
            break;
          }
          case 1: {
            deb = deb + lc.getValor();
            break;
          }
        }
        l.append(lc.getConta().getNomeConta() + " - "
                + new DataString(lc.getData()).getData() + " - "
                + lc.getValor(), null);
      }
      l.append("  Créditos: " + cred, null);
      l.append("  Débitos: " + deb, null);
      l.append("  Saldo: " + (cred - deb), null);
    } catch (FloggyException ex) {
      msgErro(ex.getMessage());
    }

    switchDisplayable(null, l);
  }
}
